package cn.krui.helper.tools;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author zc
 */
public class DateTimeUtils {
  private Calendar calendar;
  
  public DateTimeUtils() {
    this.calendar = Calendar.getInstance();
  }
  
  public DateTimeUtils(Date date) {
    this.calendar = Calendar.getInstance();
    this.calendar.setTime(date);
  }
  
  public DateTimeUtils(Calendar calendar) {
    this.calendar = calendar;
  }
  
  public DateTimeUtils(String date) throws ParseException {
    int ch1 = date.indexOf('-');
    int ch2 = date.indexOf('/');
    int ch3 = date.indexOf(':');
    SimpleDateFormat sdateFmt = null;
    if (ch1 > 0 && ch3 > 0) {
      sdateFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    } else if (ch2 > 0 && ch3 > 0) {
      sdateFmt = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
    } else if (ch3 > 0) {
      sdateFmt = new SimpleDateFormat("HH:mm:ss");
    } else {
      throw new ParseException("", 0);
    } 
    this.calendar = Calendar.getInstance();
    this.calendar.setTime(sdateFmt.parse(date));
  }
  
  public DateTimeUtils(String date, String fmt) throws ParseException {
    SimpleDateFormat sdateFmt = new SimpleDateFormat(fmt);
    this.calendar = Calendar.getInstance();
    this.calendar.setTime(sdateFmt.parse(date));
  }
  
  public DateTimeUtils date() {
    this.calendar.set(11, 0);
    this.calendar.set(12, 0);
    this.calendar.set(13, 0);
    this.calendar.set(14, 0);
    return this;
  }
  
  public DateTimeUtils addYear(int year) {
    this.calendar.add(1, year);
    return this;
  }
  
  public DateTimeUtils addMonth(int month) {
    this.calendar.add(2, month);
    return this;
  }
  
  public DateTimeUtils addDays(int days) {
    this.calendar.add(5, days);
    return this;
  }
  
  public DateTimeUtils addHours(int hours) {
    this.calendar.add(10, hours);
    return this;
  }
  
  public DateTimeUtils addMinutes(int minutes) {
    this.calendar.add(12, minutes);
    return this;
  }
  
  public DateTimeUtils addSeconds(int seconds) {
    this.calendar.add(13, seconds);
    return this;
  }
  
  public DateTimeUtils addMilliSeconds(int milliSeconds) {
    this.calendar.add(14, milliSeconds);
    return this;
  }
  
  public DateTimeUtils setYear(int year) {
    this.calendar.set(1, year);
    return this;
  }
  
  public DateTimeUtils setMonth(int month) {
    this.calendar.set(2, month);
    return this;
  }
  
  public DateTimeUtils setDays(int day) {
    this.calendar.set(5, day);
    return this;
  }
  
  public DateTimeUtils setHours(int hours) {
    this.calendar.set(10, hours);
    return this;
  }
  
  public DateTimeUtils setSeconds(int seconds) {
    this.calendar.set(13, seconds);
    return this;
  }
  
  public DateTimeUtils setMilliSeconds(int milliSeconds) {
    this.calendar.set(14, milliSeconds);
    return this;
  }
  
  public Date toDate() {
    return this.calendar.getTime();
  }
  
  public Calendar toCalendar() {
    return this.calendar;
  }
  
  public String toString(String fmt) {
    SimpleDateFormat sdateFmt = new SimpleDateFormat(fmt);
    return sdateFmt.format(this.calendar.getTime());
  } 
}
